/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics.ga.operators;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.epanetgrid.otimizacao.simulation.GradienteNaoConvergeOtimizacaoException;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Operator;


/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public class CompositeOperator implements Operator, Serializable {
    private List operators;

    /**
     * TODO
     */
    public CompositeOperator() {
        operators = createList();
    }

    /**
     * TODO
     *
     * @param status
     * @param config
     * @see org.jheuristics.ga.Operator#operate(org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
     */
    public void operate(GAStatus status, GAConfig config) throws GradienteNaoConvergeOtimizacaoException {
        for (Iterator it = operators.iterator(); it.hasNext();) {
            ((Operator) it.next()).operate(status, config);
        }
    }

    /**
     * TODO
     *
     * @param operator
     */
    public void addOperator(Operator operator) {
        if (null == operator) {
			// TODO: ERROR MSG
            throw new NullPointerException();
        }
        operators.add(operator);
    }

    /**
     * TODO
     *
     * @param index
     * @param operator
     */
    public void addOperator(int index, Operator operator) {
        if (null == operator) {
			// TODO: ERROR MSG
            throw new NullPointerException();
        }
        if (0 > index || operators.size() < index) {
			// TODO: ERROR MSG
            throw new IllegalArgumentException();
        }
        operators.add(index, operator);
    }

    /**
     * TODO
     *
     * @param operator
     */
    public void removeOperator(Operator operator) {
        if (null == operator) {
			// TODO: ERROR MSG
            throw new NullPointerException();
        }
        operators.remove(operator);
    }

    /**
     * TODO
     *
     * @param operator
     * @param index
     */
    public void removeOperator(int index) {
        if (0 > index || operators.size() <= index) {
			// TODO: ERROR MSG
            throw new IllegalArgumentException();
        }
        operators.remove(index);
    }

    /**
     * TODO
     */
    public void clear() {
        operators.clear();
    }

    /**
     * TODO
     *
     * @return
     */
    private List createList() {
        return new Vector();
    }

}
